package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.analyse.AnalyseNews;

/**
 * http://www.blogbus.com 博客大巴(上海)
 * @author abc
 */
public class BlogbusBlogAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		//http://www.blogbus.com/fengshang/2013123121928.html
		String[] regex = {
				"http://[a-z0-9]*.blogbus.com/logs/[0-9]*.html",
				"http://.*.blogbus.com/[a-z]*/[0-9]*.html",
				"http://blogger.blogbus.com/logs/[0-9]*.html",
				""
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		String html = urlMeta.getHtml();
		if (html== null) {
		}
		NewsMeta blog = new NewsMeta();
		blog.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String author=null;
		Document doc = Jsoup.parse(html);
		title  = doc.select("div#detail div.postHeader h2").text();
		author = doc.select("div.postHeader > div.menubar > div.info> a:eq(0)").text();
		String datestr = doc.select("div#detail div.postHeader h5").text();
		if (datestr.trim().length()==0 || datestr==null) {
			datestr = doc.select("div#outerContent div#content ul#posts li div.postHeader h3").text();
		}
		date = DateUtils.matchDate(datestr);
		if (date==null){
			date = DateUtils.matchDate(doc.select("span.time").text());
			if (date==null){
				date = DateUtils.matchDate(doc.select("ul#posts div.postHeader div.postsTop").text());
			}
		}
		content = HtmlCleaner.getContentHtml(url,  doc.select("div#detail div.postBody"));
		if(StringUtils.isBlank(title)){
		}
		String textDeliver = doc.select("div#detailWrapper div#detail div.postFooter div.related script").attr("src");
		if (textDeliver.trim().length()>0) {
			blog.setUpdateUrl(textDeliver);
		}
		blog.setTitle(title);
		blog.setContent(content);
		blog.setDate(date);
		blog.setAuthor(author);
		NewsMeta update = Update(blog);
		if (update!=null) {
			blog.setClickNum(update.getClickNum());
			blog.setCommentNum(update.getCommentNum());
			//blog.setDeliverNum(update.getDeliverNum());
		}
		return blog;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				Integer clickNum=0;
				Integer commentNum=0;
				Integer deliverNum=0;
				String html = CrawlHTML.GetHtml(updateUrl);
				if (html.trim().length()>0) {
					int index = html.indexOf("\"count\":\"");
					int index2 = html.indexOf("\",\"index");
					try {
						html = html.substring(index+9,index2);
						deliverNum = Integer.parseInt(html);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				meta.setClickNum(clickNum);
				meta.setCommentNum(commentNum);
				//meta.setDeliverNum(deliverNum);
				return meta;
			}
		}
		return null;
	}





	

	
}
